package com.infore.airsupervise.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.infore.airsupervise.R;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.app.MsgConst;
import com.infore.airsupervise.bean.SearchContainE;
import com.infore.airsupervise.bean.UserE;
import com.infore.airsupervise.biz.impl.UserAndGridBizImpl;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.ui.widget.SearchContainView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * @features: 人员选择（通讯录）
 * @author: WuTao
 * @date: 2017/5/8 09:16
 */
public class PsonContactActivity extends BaseTitleBarActivity {
    @BindView(R.id.pson_list)
    SearchContainView psonListView;
    private UserAndGridBizImpl userAndGridBiz;
    private List<SearchContainE> allPsonList= new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pson_selector);
        ButterKnife.bind(this);
        userAndGridBiz = new UserAndGridBizImpl();
        initViews();
        if(null ==UserE.getAllPson() || UserE.getAllPson().size()<=0){
            getAllPsonData();
        }else {
            setPsonSearchList(UserE.getAllPson());
        }
    }

    private void initViews() {
        title.setText("人员选择");
        rightMenu.setVisibility(View.INVISIBLE);
        psonListView.setSearchContainViewListener(new SearchContainView.SearchContainViewListener() {
            @Override
            public void onClickListener(View v) {
                finish();
            }
        });
        psonListView.setSearchContainListener(new SearchContainView.SearchContainItemListener() {
            @Override
            public void onItemClickListener(SearchContainE item) {
                UserE userE = (UserE) item.getDate();
                Intent it = new Intent();
                it.putExtra(Constants.INTENT_OBJ_KEY,userE);
                setResult(RESULT_OK,it);
                finish();
            }
        });

    }
    /**
     * 设置人员信息
     * @param userEList
     */
    private void setPsonSearchList(List<UserE> userEList){
        if (null != userEList && userEList.size() > 0) {
            for(UserE userE : userEList){
                allPsonList.add(new SearchContainE(userE, userE.getRealName(), ""));
            }
        }
        psonListView.setItemData(allPsonList);
        psonListView.updata();
    }
    /**
     * 服务器 获取人员信息
     */
    private void getAllPsonData() {
        if (null == userAndGridBiz) {
            userAndGridBiz = new UserAndGridBizImpl();
        }
        //loading dialog
        final SweetAlertDialog dialog = new SweetAlertDialog(ctx, SweetAlertDialog.PROGRESS_TYPE)
                .setTitleText(MsgConst.HARD_LOADING)
                .setContentText(MsgConst.PLEASE_WAITING);
        dialog.setCancelable(false);
        dialog.show();
        userAndGridBiz.getAllPsonRequest(UserE.getUserInfo().getUserId(),
                new OnServerResponseListener<List<UserE>>() {
                    @Override
                    public void onSucceed(List<UserE> result, String desc) {
                        //dialog 隐藏
                        dialog.dismiss();
                        UserE.saveAllPson(result);//保存本地
                        setPsonSearchList(result);//设置人员信息
                        psonListView.updata();
                    }
                    @Override
                    public void onFailed(int errorCode, String title, String desc) {
                        //dialog 错误 异常提示
                        dialog.setTitleText(title)
                                .setContentText(desc)
                                .setConfirmText(MsgConst.I_SEE)
                                .changeAlertType(SweetAlertDialog.WARNING_TYPE);
                    }
                });
    }
}
